package com.study.talk.module.netty.handler;

import com.study.talk.module.entity.UserInfo;
import com.study.talk.module.netty.message.Attributes;
import com.study.talk.module.netty.message.Session;
import com.study.talk.module.netty.message.request.AcceptRequest;
import com.study.talk.module.netty.message.response.AcceptResponse;
import com.study.talk.module.netty.message.response.MessageResponse;
import com.study.talk.module.netty.message.response.MessageSelfResponse;
import com.study.talk.module.service.FriendService;
import com.study.talk.module.service.InviteUserRecordService;
import com.study.talk.module.service.UserInfoService;
import com.study.talk.module.service.UserService;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 同意添加好友消息处理
 */


@Component
@Slf4j
@ChannelHandler.Sharable
public class AcceptAddUserRequestHandler extends SimpleChannelInboundHandler<AcceptRequest> {

    @Resource
    private UserService userService;

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private InviteUserRecordService inviteUserRecordService;

    @Resource
    private FriendService friendService;

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, AcceptRequest msg) throws Exception {
        log.info("同意添加好友事件,{}",msg);

        AcceptResponse acceptResponse = new AcceptResponse();
        Session session = ctx.channel().attr(Attributes.SESSION).get();
        UserInfo userInfo = userInfoService.getById(session.getUserId());
        acceptResponse.setInvitedName(userInfo.getNickName());
        acceptResponse.setInvitedUserId(userInfo.getId());
        acceptResponse.setInviteUserId(msg.getRequestUserId());

        acceptResponse.setDateTime(msg.getDateTime());
        acceptResponse.setSuccess(msg.getAccept());

        //删除好友邀请记录
        inviteUserRecordService.removeInviteFriend(msg.getRequestUserId(),session.getUserId());
        //如果同意添加为好友
        if (acceptResponse.getSuccess()){
            //双向添加好友
            friendService.addFriend(msg.getRequestUserId(),session.getUserId());
            friendService.addFriend(session.getUserId(),msg.getRequestUserId());
        }
        //发送响应
        userService.sendMessage(msg.getRequestUserId(),acceptResponse);
        //发送消息给添加好友请求方
        {
            MessageResponse response = new MessageResponse();
            response.setUserId(session.getUserId());
            response.setContent("我们已经是好友了，快来聊天吧！");
            response.setMessageType(0);
            response.setNickname(userInfo.getNickName());
            response.setAvatar(userInfo.getAvatar());
            response.setDateTime(msg.getDateTime());
            response.setUsername(userInfo.getUsername());
            userService.sendMessage(msg.getRequestUserId(),response);

            MessageSelfResponse messageSelfResponse = new MessageSelfResponse(); //相互发送一条消息开始聊天
            messageSelfResponse.setSuccess(true);
            messageSelfResponse.setReceiverId(msg.getRequestUserId());
            messageSelfResponse.setDateTime(msg.getDateTime());
            messageSelfResponse.setMessageType(0);
            messageSelfResponse.setContent("我们已经是好友了，快来聊天吧！");
            userService.sendMessage(session.getUserId(),messageSelfResponse);
        }
    }
}
